<?php
namespace App\Http\Controllers\Sale;

use App\Models\SaleUserLogin;
use App\Models\SaleUser;
use App\Models\SaleLog;
use App\Services\AuthService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use App\Services\CommonService;
use App\Services\UploadService;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{
    /**
     * 登陆
     */
    public function login(Request $request ){
        $mobile=$request->input('mobile');
        $password=$request->input('password');
        if(!$mobile || !$password){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'账号或密码不能为空');
        }

        $user = SaleUserLogin::where(['mobile'=>$mobile,'is_show'=>CommonService::SHOP_ENABLE])->first();
        $user = obToArray($user);

        if(!$user){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'帐号不存在或异常');
        }
        $sale_user = SaleUser::where(['sale_user_id'=>$user['sale_user_id'],'is_show'=>CommonService::SHOP_ENABLE,'is_check'=>CommonService::SHOP_ENABLE])->first();
        $sale_user = obToArray($sale_user);

        if($user && $sale_user){
            if(md5($mobile.$password) == $user['password']){
                $token = createToken(AuthService::MODEL_SALE);
                $res = SaleUserLogin::where('id',$user['id'])->update(['token'=>$token]);
                if($res){
                    Redis::del($user['token']);
                    $user['token'] = $token;
                    Redis::setEx($token, 1800, json_encode($user));
                    $data = [];
                    $data['sale_user_id'] = $user['sale_user_id'];
                    $data['user_id'] = $user['id'];
                    $data['content'] = '分销商'.$user['username'].'登录';
                    $data['remark'] = '';
                    SaleLog::addLog($data);
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'登录成功',[
                        'token'=>$token,
                        'username'=>$user['username'],
                        'sale_user_id'=>$user['sale_user_id']
                        ]);
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'登录失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'密码错误');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'帐号不存在或异常');
        }
    }

    /**
     * 退出登陆
     */
    public function loginOut(Request $request){
        $userToken = $request->header("SaleAuth");
        if($userToken){
            $user = SaleUserLogin::where(['token'=>$userToken,'is_show'=>CommonService::SHOP_ENABLE])->first();
            $user = obToArray($user);
            if($user){
                Redis::del($userToken);
                SaleUserLogin::where('id',$user['id'])->update(['token'=>'']);
                $data = [];
                $data['sale_user_id'] = $user['sale_user_id'];
                $data['user_id'] = $user['id'];
                $data['content'] = '分销商'.$user['username'].'退出登录';
                $data['remark'] = '';
                SaleLog::addLog($data);
            }

        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'退出成功');
    }

    /**
     * 忘记密码
     * @param Request $request
     * @return false|string
     */
    public function forgetPwd(Request $request){
        $mobile = $request->input('contact_tel','');
        $code = $request->input('regustCode','');
        $pwd = $request->input('password','');

        if(!$mobile ||!$code || !$pwd){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }

        $checkUser  = SaleUserLogin::where(['mobile'=>$mobile])->select('id','mobile','sale_user_id','username')->first();
        $checkUser = $checkUser?$checkUser->toArray():[];
        if(!$checkUser){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'帐号不存在或异常');
        }
        $checkCode = SmsService::checkCode($mobile,$code);
        if(!$checkCode){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
        }
        $data['password'] = md5($checkUser['mobile'].$pwd);
        $res = SaleUserLogin::where(['sale_user_id'=>$checkUser['sale_user_id']])->update($data);
        if($res!=='false'){
            $data = [];
            $data['sale_user_id'] = $checkUser['sale_user_id'];
            $data['user_id'] = $checkUser['id'];
            $data['content'] = '分销商'.$checkUser['username'].'忘记密码操作更改密码';
            $data['remark'] = '';
            SaleLog::addLog($data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    public function upload(Request $request){
        $sizeLimit = $request->input('sizeLimit','0');
        $riginalName = $request->input('riginalName','0');
        $res = UploadService::upload($sizeLimit,$riginalName);
        return ReturnMsg($res['status'] ? CommonService::HTTP_CODE_SUCCESS : CommonService::HTTP_CODE_FAIL,$res['message'],$res['status'] ? ['image'=>$res['image']] : []);
    }

    /**
     * 发送短信验证码
     * @param Request $request
     * @return false|string
     */
    public function sendCode(Request $request){
        $mobile = $request->input('mobile');
        // dd($mobile);
        if(!$mobile){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $checkUser  = SaleUserLogin::where(['mobile'=>$mobile])->select('id','mobile','sale_user_id','username','is_show')->first();
        $checkUser = $checkUser?$checkUser->toArray():[];
        // if(!$checkUser){
        //     return ReturnMsg(CommonService::HTTP_CODE_FAIL,'用户不存在');
        // }
        // if(CommonService::SHOP_DISABLE == $checkUser['is_show']){
        //     return ReturnMsg(CommonService::HTTP_CODE_FAIL,'账号未启用');
        // }
        $res = SmsService::sendCode($mobile);
        if($res['status']){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,$res['msg']);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['msg']);
        }
    }

}


